#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
using namespace std;
int main(){
  extern char** environ;
  pid_t id = fork();
  if (id == 0){
    //child
    cout << "I am a child process: " << getpid() << endl;
   // execl("/bin/ls", "ls", "-a", "-l", NULL);
   
   //程序替换失败
    //int n = execl("/bin/lssss", "lssss", "-a", "-l", NULL);
    //cout << "exchange flase: " << n << endl;
    //exit(0);
    
    //不用接收返回值
    //execl("/bin/lssss", "lssss", "-a", "-l", NULL);
    //exit(1);
    
    //ls hello.txt文件 
    //execl("/bin/ls", "ls", "hello.txt", NULL);
    //exit(1);
    
    //传入数组
    //char *const myargv[] = {
    //  "ls",
    //  "-a",
    //  "-l",
    //  "-n",
    //  NULL
    //};
    //execv("/bin/ls", myargv);
    
    //不带路径执行的接口
    //execlp("ls", "ls", "-a", "-l", "-n", NULL);
    
    //传输组不带路径
    //execvp("ls", myargv);
    
    //使用自己的程序
    //char* const myenv[] = {
    //  "MYENV=YouCanSeeMe",
    //  NULL 
    //};

    //将MYENV环境变量导入其中
    //putenv("MYENV=YouCanSeeMe");
    //execle("./exec/otherproc", "otherproc", NULL, environ);
    //execl("./exec/shell.sh", "shell.sh", NULL);
    execl("./exec/test.py", "test.py", NULL);
    exit(1);
  }

  sleep(1);

  //father
  int status = 0;
  cout << "I am a father process: " << getpid() << endl;
  waitpid(id, &status, 0);
  cout << "child exit code: " << WEXITSTATUS(status) << endl;

  return 0;
}
